/*
 *  Created on: Nov 15, 2012
 *      Author: Andrey Belomutskiy
 */

#include <string.h>
#include "rfiutil.h"

int tokenLength(char *msgp) {
	int result = 0;
	while (*msgp) {
		char ch = *msgp++;
		if (ch == ' ')
			break;
		result++;
	}
	return result;
}

int strEqual(char *str1, char *str2) {
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	if (len1 != len2)
		return FALSE;
	int i;
	for (i = 0; i < len1; i++)
		if (str1[i] != str2[i])
			return FALSE;
	return TRUE;
}

char *ltoa(char *p, long num, unsigned radix) {
	int i;
	char *q;

	q = p + _MAX_FILLER;
	do {
		i = (int) (num % radix);
		i += '0';
		if (i > '9')
			i += 'A' - '0' - 10;
		*--q = i;
	} while ((num /= radix) != 0);

	i = (int) (p + _MAX_FILLER - q);
	do
		*p++ = *q++;
	while (--i);

	return p;
}

char *sput_i(int, char *);
char *sput_ip1(int, char *);

void itoa(int integer, char *string) {
	if (0 > integer) {
		++integer;
		*string++ = '-';
		*sput_ip1(-integer, string) = '\0';
	} else {
		*sput_i(integer, string) = '\0';
	}
}

/**
 * writes the decimal representation of a nonnegative int value
 * argument to an array of char.
 */
char *sput_i(int integer, char *string) {
	if (integer / 10 != 0) {
		string = sput_i(integer / 10, string);
	}
	*string++ = (char) ('0' + integer % 10);
	return string;
}

/**
 *  writes the representation of one greater than the
 *  argument value, for any nonnegative int value argument, to an
 *  array of char.
 */
char *sput_ip1(int integer, char *string) {
	int digit;

	digit = (integer % 10 + 1) % 10;
	if (integer / 10 != 0) {
		string = (digit == 0 ? sput_ip1 : sput_i)(integer / 10, string);
		*string++ = (char) ('0' + digit);
	} else {
		if (digit == 0) {
			*string++ = '1';
		}
		*string++ = (char) ('0' + digit);
	}
	return string;
}

